package pe.com.hermes.hermetic.authentication.handlers;

import pe.com.hermes.hermetic.beans.User;

import pe.com.hermes.hermetic.negocio.ultil.ParameterHermetic;
import pe.com.hermes.hermetic.negocio.ultil.Util;

import java.io.IOException;

import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import pe.com.hermes.hermetic.negocio.ultil.ConstantesHermetic;

public class HWLogoutSuccessHandler implements LogoutSuccessHandler {    
    private Logger logger = Logger.getLogger(this.getClass());
    private static String TARGET_URL_SESION_FINISHED = "/j_spring_security_logout";    
    //private static String TARGET_URL_SESION_FINISHED = "/login.html";    
    
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
                     Authentication authentication) throws IOException, ServletException {
               
            String targetUrl="";
            
            try{
                User clienteBean = (User)authentication.getPrincipal();
                boolean isRoleBCP = Util.hasRole("ROLE_BCP", (User)authentication.getPrincipal());  
                
                ResourceBundle resource = ResourceBundle.getBundle("port-config");
                String portNumberException = resource.getString("portNumber");  
                String dominioBCP = resource.getString("dominioBCP");
                HttpSession session = request.getSession(false);
                String desDireCtrlAccesoWeb = ParameterHermetic.URL_MODU_CTRL_ACCE;
                          
             if(Util.hasPortException(String.valueOf(clienteBean.getCodRazoSoci())))
                //targetUrl = request.getScheme() + "://"+ request.getServerName() + portNumberException + request.getContextPath() + TARGET_URL_SESION_FINISHED;                 
                targetUrl = ConstantesHermetic.PROTOCOLO_HTTPS + "://"+ request.getServerName() + portNumberException + "/.."+ desDireCtrlAccesoWeb + TARGET_URL_SESION_FINISHED;                 
             else
                 //logica redireccion BCP
                 if (isRoleBCP)
                    targetUrl = ConstantesHermetic.PROTOCOLO_HTTPS + "://"+  dominioBCP  + ":" + ConstantesHermetic.PORT_HTTPS + request.getContextPath() + "/.."+ desDireCtrlAccesoWeb + TARGET_URL_SESION_FINISHED;                                     
                 else             
                    targetUrl = ConstantesHermetic.PROTOCOLO_HTTPS + "://"+ request.getServerName() + ":" + ConstantesHermetic.PORT_HTTPS + "/" + desDireCtrlAccesoWeb + TARGET_URL_SESION_FINISHED;
                 
            }catch(Exception e){
                e.printStackTrace();
            }
            logger.debug("Redirecting to HWLogoutSuccessHandler Url: " + targetUrl);                
            redirectStrategy.sendRedirect(request, response, targetUrl);    
     }

    public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
        this.redirectStrategy = redirectStrategy;
    }
}
